<template>
	<el-dialog
		title="提示"
		:visible.sync="$store.state.Users.addUser"
		width="36%"
		@close="cancle"
	>
		<!-- 表单验证组件 -->
		<slot />

		<span slot="footer" class="dialog-footer">
			<el-button @click="dialogVisible">取 消</el-button>
			<el-button type="primary" @click="dialogVisibled">确 定</el-button>
		</span>
	</el-dialog>
</template>
<script>
export default {
	name: 'Dialog',
	data() {
		return {}
	},
	props: ['start'],
	methods: {
		dialogVisible() {
			this.$store.commit('Users/Add')
		},
		dialogVisibled() {
			this.$store.commit('Users/Add')
			this.$bus.$emit('addUser')
		},
		cancle() {
			this.$bus.$emit('cancle')
		},
	},
	components: {},
	mounted() {
		this.$bus.$on('isData', (data, listData) => {
			if (!data) {
				this.$message.error('添加用户失败')
				return
			} else {
				this.$http
					.post('/users', listData)
					.then(response => {
						const { meta } = response.data
						if (meta.status == 201) {
							this.$bus.$emit('getDatas')
							this.$message({
								message: '添加用户成功',
								type: 'success',
							})
						} else {
							this.$message.error('添加用户失败')
						}
					})
					.catch(error => {
						console.log(error.message)
					})
			}
		})
	},
}
</script>
